package com.asura.app.wechat.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.asura.app.library.utils.CollectionUtils;
import com.asura.app.wechat.R;
import com.asura.app.wechat.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Asura on 2017/12/28.
 */

public class NineImageGridLayout extends ViewGroup {

    private int space;
    private int imageSize;
    private List<String> imageUrls;

    public NineImageGridLayout(Context context) {
        this(context, null);
    }

    public NineImageGridLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NineImageGridLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray t = context.obtainStyledAttributes(attrs, R.styleable.NineImageGridLayout);
        space = (int) t.getDimension(R.styleable.NineImageGridLayout_space, UIUtils.dip2px(context, 2));
        for (int i = 0; i < 9; i++) {
            NetworkImageView imageView = new NetworkImageView(getContext());
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setVisibility(GONE);
//            imageView.setImageResource(R.drawable.test_avatar);
            addView(imageView);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int measuredWidth = getMeasuredWidth();
        int realHeight = getPaddingTop() + getPaddingBottom();
        if (measuredWidth > 0) {
            imageSize = (measuredWidth - space * 2 - getPaddingLeft() - getPaddingRight()) / 3;
            int visibleCount = 0;
            for (int i = 0; i < 9; i++) {
                View child = getChildAt(i);
                child.measure(MeasureSpec.makeMeasureSpec(imageSize, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(imageSize, MeasureSpec.EXACTLY));
                if (child.getVisibility() != View.GONE) {
                    visibleCount++;
                }
            }
            if (visibleCount > 0) {
                realHeight += imageSize;
            }
            if (visibleCount > 3) {
                realHeight += imageSize + space;
            }
            if (visibleCount > 6) {
                realHeight += imageSize + space;
            }
            setMeasuredDimension(measuredWidth, realHeight);
        }

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        for (int i = 0; i < getChildCount(); i++) {
            ImageView imageView = (ImageView) getChildAt(i);
            int row = i / 3;
            int column = i % 3;
            int left = column * imageSize + column * space + getPaddingLeft();
            int top = row * imageSize + row * space + getPaddingTop();
            imageView.layout(left, top, left + imageSize, top + imageSize);
        }
    }

    public void setImageUrls(List<String> imageUrls) {
        if (CollectionUtils.isEmpty(imageUrls) || imageUrls.size() > 9) {
            throw new IllegalArgumentException("only support at most 9 pictures");
        }
        if (imageUrls.equals(this.imageUrls)) {
            return;
        }
        this.imageUrls = imageUrls;
        List<NetworkImageView> imageViews = new ArrayList<>();
        if (imageUrls.size() == 4) {
            setVisibility(true, getChildAt(0), getChildAt(1), getChildAt(3), getChildAt(4));
            setVisibility(false, getChildAt(2), getChildAt(5), getChildAt(6), getChildAt(7), getChildAt(8));
            imageViews.add((NetworkImageView) getChildAt(0));
            imageViews.add((NetworkImageView) getChildAt(1));
            imageViews.add((NetworkImageView) getChildAt(3));
            imageViews.add((NetworkImageView) getChildAt(4));
        } else {
            for (int i = 0; i < imageUrls.size(); i++) {
                NetworkImageView imageView = (NetworkImageView) getChildAt(i);
                imageView.setVisibility(VISIBLE);
                imageViews.add(imageView);
            }
            for (int i = imageUrls.size(); i < getChildCount(); i++) {
                getChildAt(i).setVisibility(View.GONE);
            }
        }
        for (int i = 0; i < imageViews.size(); i++) {
            imageViews.get(i).load(imageUrls.get(i));
        }
    }

    private void setVisibility(boolean visible, View... views) {
        for (View view : views) {
            view.setVisibility(visible ? VISIBLE : GONE);
        }
    }
}
